package com.bracketbird.server.repository.tournament;

import com.bracketbird.client.model.tournament.*;
import com.bracketbird.server.jdo.club.*;
import com.bracketbird.server.repository.*;
import com.google.appengine.api.datastore.*;

import java.util.*;


/**
 *
 */
public class TournamentLevelConverter extends Converter<TournamentJDO, Tournament, CreateTournament> {


    public TournamentJDO convert(CreateTournament cr) {
        if (cr == null) {
            return null;
        }

        TournamentJDO jdo = new TournamentJDO();
        Key tKey = KeyFac.createTournamentKey(cr.getClubId());
        jdo.setKey(tKey);

        jdo.setClubKey(KeyFac.convert(cr.getClubId()));
        jdo.setCreatedDate(new Date());
        jdo.setTemplate(cr.getTemplate());
        jdo.setName(cr.getNameOfTournament());

        return jdo;
    }


    public TournamentLevel convert(TournamentLevelJDO jdo) {
        TournamentLevel model = null;//TournamentLevelFac.create(t, jdo.getType());

        model.setId(KeyFac.getSubtournamentId(jdo.getKey()));
        model.setCreatedDate(jdo.getCreatedDate());
        model.setLastChangeDate(jdo.getLastChangeDate());
        model.setTournamentId(KeyFac.getTournamentId(jdo.getTournamentKey()));

        //model.setTeams(convertPlayingTeamKeys(jdo.getTeams()));
        LevelSettings ss = new LevelSettings();

        ss.setEliminationType(jdo.getEliminationType());
        ss.setMaxNumberOfTeams(jdo.getMaxNumberOfTeams());
        ss.setNumberOfMatches(jdo.getNumberOfMatches());
        ss.setNumberOfGroups(jdo.getNumberOfGroups());
        ss.setNumberOfRepeats(jdo.getNumberOfRepeats());
        ss.setPointsOfDraw(jdo.getPointsOfDraw());
        ss.setPointsOfVictory(jdo.getPointsOfVictory());
        ss.setRankingRules(jdo.getRankingRules());

        model.setStageSettings(ss);
        //TODO - matches
        return model;
    }

    public Tournament convert(TournamentJDO jdo) {
        if (jdo == null) {
            return null;
        }
        Tournament t = new Tournament();
        t.setId(KeyFac.getTournamentId(jdo.getKey()));
        t.setClubId(KeyFac.getClubId(jdo.getClubKey()));
        t.setLastChangeDate(jdo.getLastChangeDate());
        t.setCreatedDate(jdo.getCreatedDate());
        t.setName(jdo.getName());
        return t;
    }

    @Override
    public void updateJDO(TournamentJDO jdo, Tournament model) {
        jdo.setName(model.getName());
        //TODO - shouldnt be called

    }

    public void updateJDO(TournamentLevelJDO jdo, TournamentLevel model) {
        jdo.setEliminationType(model.getStageSettings().getEliminationType());
        jdo.setMaxNumberOfTeams(model.getStageSettings().getMaxNumberOfTeams());
        jdo.setNumberOfMatches(model.getStageSettings().getNumberOfMatches());
        jdo.setNumberOfGroups(model.getStageSettings().getNumberOfGroups());
        jdo.setNumberOfRepeats(model.getStageSettings().getNumberOfRepeats());
        jdo.setPointsOfDraw(model.getStageSettings().getPointsOfDraw());
        jdo.setPointsOfVictory(model.getStageSettings().getPointsOfVictory());
        jdo.setRankingRules(model.getStageSettings().getRankingRules());
        //jdo.setTeams(convertIds(model.getStageSettings().getTeams()));

        //jdo.setMatches(convert); TODO

    }



}